﻿using ctolua.Models.System;
using ctolua.Models.Type;
using static ctolua.Data.slk;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace ctolua.Models
{
    public class MItem
    {
        /// <summary>
        /// 物品实体
        /// </summary>
        public Item item { get; set; }
        /// <summary>
        /// id
        /// </summary>
        public int id => (int)this.item.api_get_id();
        /// <summary>
        /// item转字符串
        /// </summary>
        public string id_s;
        /// <summary>
        /// 编号
        /// </summary>
        public int? key => this.item.api_get_key();
        /// <summary>
        /// 物品昵称
        /// </summary>
        public string name => this.item.get_name();
        /// <summary>
        /// 预设 物品昵称
        /// </summary>
        public string default_name => game_api.get_item_name_by_type(this.key);
        /// <summary>
        /// 物品描述
        /// </summary>
        public string desc => this.item.api_get_desc();
        /// <summary>
        /// 预设 物品描述
        /// </summary>
        public string default_desc => game_api.get_item_desc_by_type(this.key);
        /// <summary>
        /// 物品等级
        /// </summary>
        public int? level => this.item.api_get_level();
        /// <summary>
        /// 物品类型
        /// </summary>
        public int? type => this.item.api_get_type();
        /// <summary>
        /// icon图标的图片id
        /// </summary>
        public int? icon => game_api.get_icon_id_by_item_type(this.key);
        /// <summary>
        /// 获取物品商品售价
        /// </summary>
        /// <param name="res_type"> 资源类型 </param>
        /// <returns>售价</returns>
        public double money(string res_type) => game_api.get_item_buy_price(this.key, res_type).@float();
        /// <summary>
        /// 获取最近创建的物品
        /// </summary>
        /// <returns>物品对象</returns>
        public MItem get_last_created_item => new MItem(game_api.get_last_created_item());
        /// <summary>
        /// 获取物品配置名称
        /// </summary>
        /// <returns>物品名称</returns>
        public string get_item_conf_name => game_api.get_item_conf_name(this.key);
        public bool is_null => (bool)game_api.item_is_exist(this.item);
        /// <summary>
        /// 物品缩放
        /// </summary>
        /// <param name="size"></param>
        public void set_scale(double size) => this.item.api_set_scale(size);
        /// <summary>
        /// 转对象 带缓存
        /// </summary>
        /// <param name="unit_id">物品ID</param>
        /// <returns></returns>
        public static MItem obj(int item_id)
        {
            string item = py.toString(game_api.get_item(item_id));
            MItem mItem = SCache.getItemCache<MItem>(item);
            if (mItem == null)
            {
                mItem = new MItem((int)item_id);
                SCache.setItemCache(item, mItem);
                return mItem;
            }
            return mItem;
        }
        /// <summary>
        /// 转对象 带缓存
        /// </summary>
        /// <param name="i">物品对象</param>
        /// <returns></returns>
        public static MItem? obj(Item i)
        {
            if (i==null)
            {
                return null;
            }
            string item = py.toString(i);
            
            MItem mItem = SCache.getItemCache<MItem>(item);
            if (mItem == null)
            {
                mItem = new MItem(i);
                SCache.setItemCache(item, mItem);
                return mItem;
            }
            return mItem;
        }
        /// <summary>
        /// 创建单个物品
        /// </summary>
        /// <param name="position ">  位置   </param>
        /// <param name="item_key ">  物品编号   </param>
        /// <param name="player ">  所属玩家   </param>
        public static MItem create(Point position, int? item_key, Role player)
        {
            MItem mItem = new MItem(position, item_key, player);
            SCache.setItemCache(py.toString(mItem.item), mItem);
            return mItem;
        }
        public MItem() { }
        public MItem(Item item)
        {
            this.item = item;
            this.id_s = py.toString(this.item);
        }
        /// <summary>
        /// id转物品
        /// </summary>
        /// <param name="id"></param>
        public MItem(int id)
        {
            this.item = game_api.get_item(id);
            this.id_s = py.toString(this.item);
        }
        /// <summary>
        /// 创建单个物品
        /// </summary>
        /// <param name="position ">  位置   </param>
        /// <param name="item_key ">  物品编号   </param>
        /// <param name="player ">  所属玩家   </param>
        public MItem(Point position, int? item_key, Role player)
        {
            this.item = game_api.create_item_by_id(position, item_key, player);
            this.id_s = py.toString(this.item);
        }
        /// <summary>
        /// 创建物品 按数量
        /// </summary>
        /// <param name="position ">  位置   </param>
        /// <param name="num ">  数量   </param>
        /// <param name="eid ">  物品编号   </param>
        /// <param name="player ">  所属玩家   </param>
        public static void create_items(Point position, int? num, int? eid, Role player)
        {
            game_api.create_items(position, num, eid, player);
        }
        /// <summary>
        /// 返回指定物品名字的字符串属性值
        /// </summary>
        /// <param name="name ">  属性名   </param>
        /// <returns>字符</returns>
        public string get_item_key_str_attr(string name) => game_api.get_item_key_str_attr(this.key, name);
        /// <summary>
        /// 设置基础属性
        /// </summary>
        /// <param name="attr_key">属性key</param>
        /// <param name="val">属性值</param>
        public void api_set_attr_base(string attr_key, double? val)
        {
            this.item.api_set_attr(TAttrType.基础, attr_key,val);
        }
        /// <summary>
        /// 增加基础属性
        /// </summary>
        /// <param name="attr_key">属性key</param>
        /// <param name="val">属性值</param>
        public void api_add_attr_base(string attr_key, double? val)
        {
            this.item.api_change_attr(TAttrType.基础, attr_key, val);
        }
        /// <summary>
        /// 获取基础属性
        /// </summary>
        /// <param name="attr_key">属性key</param>
        public Fix32 api_get_attr_base(string attr_key)
        {
           return this.item.api_get_attr(TAttrType.基础, attr_key);
        }

        public override string ToString()
        {
            return $"id:{this.id} name:{this.name}";
        }
    }
}
